package com.qen.fx.custom.impl.pwd;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.BCrypt;
import com.qen.App;
import com.qen.fx.ViewIndex;
import com.qen.fx.custom.CustomBusiness;
import com.qen.fx.tools.ClipboardTool;
import com.qen.plugin.notification.NotificationTool;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

public class AddPwd extends CustomBusiness {

    @FXML
    private Slider inputSaltCount;
    @FXML
    private TextField inputString;
    @FXML
    private TextField checkStringField;
    @FXML
    private Button copyButton;
    @FXML
    private TextField outPwdField;
    @FXML
    private Label checkResult;
    @FXML
    private TextField checkHashField;
    @FXML
    private VBox leftVBox;
    @FXML
    private VBox rightVBox;
    @FXML
    private HBox hBox;
    @FXML
    private Label hashLabel;
    @FXML
    private Label checkLabel;
    @FXML
    private Label saltNumber;

    public AddPwd() {
        FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource(ViewIndex.AddPwdFXML));
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);
        try {
            fxmlLoader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String[] st = new String[]{"Binary (base 2)", "Hexadecimal (base 16)", "Base64 (base 64)", "Base64url (base 64 with url safe chars)"};
        AddPwd controller = fxmlLoader.getController();

        this.setStyle(null);
        hBox.setPrefWidth(ViewIndex.contentPreferWidth);
        leftVBox.setPrefWidth(ViewIndex.contentPreferWidth / 2 - 100);
        rightVBox.setPrefWidth(ViewIndex.contentPreferWidth / 2 - 100);
        leftVBox.setStyle("-fx-background-color: #FFF;");
        rightVBox.setStyle("-fx-background-color: #FFF;");
        rightVBox.setTranslateX(100);
        hashLabel.setPrefWidth(ViewIndex.contentPreferWidth / 2 - 300);
        checkLabel.setPrefWidth(ViewIndex.contentPreferWidth / 2 - 300);

        inputString.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observableValue, String s, String t1) {
                createPassword();
            }
        });

        inputSaltCount.valueProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observableValue, Number number, Number t1) {
                createPassword();
            }
        });

        outPwdField.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observableValue, String s, String t1) {
                checkPwd();
            }
        });

        checkHashField.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observableValue, String s, String t1) {
                checkPwd();
            }
        });

    }

    @Override
    public VBox init() {
        return null;
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {

    }

    public void createPassword() {
        String password = inputString.getText();
        double salt = inputSaltCount.getValue();
        int saltint = (int) salt;
        saltNumber.setText("盐值数(" + saltint + "):");
        if (StrUtil.isBlank(password)) {
            return;
        }
        String res = BCrypt.hashpw(password, BCrypt.gensalt(saltint));
        outPwdField.setText(res);
    }

    @FXML
    public void clickCopy(ActionEvent actionEvent) {
        String token = outPwdField.getText();
        if (StrUtil.isBlank(token)) {
            return;
        }
        ClipboardTool.copyToClipboard(token);
        NotificationTool.message("复制成功");
    }

    public void checkPwd() {
        boolean res = false;
        String password = checkStringField.getText();
        String hashedPassword = checkHashField.getText();
        if (StrUtil.isNotBlank(password) && StrUtil.isNotBlank(hashedPassword)) {
            res = BCrypt.checkpw(password, hashedPassword);
        }
        checkResult.setText(res ? "Yes" : "No");
    }
}
